<template>
	<view class="travelSearchPage">
		<view class="top_search_box">
			<view class="search_box">
				<view class="left_search_box">
					<image src="../../static/images/icon/icon_search.png" class="icon-32 mar-right-8" mode=""></image>
					<view class="search_input"><input type="text" v-model="searchText" :focus="isFocus"
							@focus="changeHistoryShow(true)" placeholder="请输入搜索内容" /></view>
				</view>
				<view class="search_btn"><button @tap="searchBtn(searchText)">搜索</button></view>
			</view>
		</view>
		<view class="search_tpye_box" v-if="showHistory">
			<view class="search_history_box">
				<view class="search_history_top_box">
					<view class="fn-sz-30">搜索历史</view>
					<image src="../../static/images/icon/icon_del_history.png" class="icon-26" mode=""
						@tap="clearHistory"></image>
				</view>
				<view class="tour_history_list">
					<view class="history_item" v-for="(item, index) in historyList" :key="index"
						@tap="searchBtn(item, true)">{{ item }}</view>
				</view>
			</view>
			<view class="hot_search">
				<view class="fn-sz-30 mar-bottom-30">热门搜索</view>
				<view class="hot_search_list">
					<view class="hot_search_item" v-for="(item, index) in hotWordList" :key="index"
						@tap="searchBtn(item, true)">{{ item }}</view>
				</view>
			</view>
		</view>
		<view class="search_goods_list" v-else>
			<TourList :list="goodsList.lists"></TourList>
			<u-loadmore :status="loadMoreStatus" v-if="goodsList.lists.length" />
		</view>
	</view>
</template>

<script>
	import TourList from '../../components/tourList.vue';
	import com from '../../mixin/index.js';
	export default {
		components: {
			TourList
		},
		mixins: [com],
		data() {
			return {
				showHistory: false,
				searchText: '',
				historyList: [],
				hotWordList: [],
				isFocus: false,
				page: {
					page: 1,
					page_size: 4
				}
			};
		},
		onPullDownRefresh() {
			this.page.page = 1;
			this.goodsList = {
				lists: [],
				total: 1,
				total_page: 0
			};
			let params = {
				...this.page,
				keyword: this.searchText
			};
			this.getList('goodsList', 'getTourList', 'page', params).then(res => {
				uni.stopPullDownRefresh();
			});
		},
		onLoad() {
			this.getHotWord();
			let oldHistoryList = uni.getStorageSync('tour_history_list');
			if (oldHistoryList) {
				this.historyList = oldHistoryList;
			} else {
				this.historyList = [];
			}
			setTimeout(() => {
				this.changeHistoryShow(true);
			}, 100);
		},
		methods: {
			changeHistoryShow(val) {
				this.showHistory = val;
				this.isFocus = val;
			},
			addHistoryList(val) {
				if (!this.historyList.includes(val)) {
					this.historyList.push(val);
					uni.setStorageSync('tour_history_list', this.historyList);
				}
			},
			searchBtn(text, type) {
				console.log(text, 'suo');
				this.page.page = 1;
				this.goodsList = {
					lists: [],
					total: 1,
					total_page: 0
				};
				this.changeHistoryShow(false);
				let params = {
					...this.page,
					keyword: text
				};
				this.getList('goodsList', 'getTourList', 'page', params);
				this.addHistoryList(text);
				if (type) {
					this.searchText = text;
				}
			},
			getHotWord() {
				this.$api.getHotWord().then(res => {
					this.hotWordList = res.data.tour;
				});
			},
			clearHistory() {
				this.historyList = [];
				uni.setStorageSync('tour_history_list', this.historyList);
			}
		}
	};
</script>

<style lang="scss">
	.travelSearchPage {
		background-color: #f9f9f9;
		min-height: 100vh;
		padding: 78rpx 0rpx 50rpx;
		box-sizing: border-box;

		.top_search_box {
			padding: 0 24rpx;
			position: fixed;
			top: 0;
			width: 100vw;
			box-sizing: border-box;
			background-color: #f9f9f9;
		}

		.search_box {
			height: 68rpx;
			background: #ffffff;
			flex: 1;
			border-radius: 34rpx;
			border: 2rpx solid #fed100;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			margin-bottom: 24rpx;

			.left_search_box {
				padding-left: 18rpx;
				display: flex;
				align-items: center;

				.search_input {
					color: #a7a7a7;

					input {
						font-size: 26rpx;
					}
				}
			}

			.search_btn {
				button {
					width: 120rpx;
					height: 68rpx;
					background: #fed100;
					font-size: 26rpx;
					border-radius: 34rpx;
				}
			}
		}

		.search_tpye_box {
			height: 100vh;
			background: #ffffff;
			border-radius: 20px 20px 0px 0px;
			padding: 40rpx 24rpx;

			.search_history_box {
				margin-bottom: 40rpx;

				.search_history_top_box {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 30rpx;
				}

				.tour_history_list {
					display: flex;
					align-items: center;
					flex-wrap: wrap;

					.history_item {
						margin-right: 35rpx;
						background: #f5f5f5;
						border-radius: 26rpx;
						color: #666666;
						padding: 8rpx 20rpx;
						font-size: 26rpx;
						font-weight: 400;
						margin-bottom: 10rpx;
					}
				}
			}

			.hot_search {
				.hot_search_list {
					display: flex;
					flex-wrap: wrap;

					.hot_search_item {
						width: 50%;
						box-sizing: border-box;
						padding-bottom: 30rpx;

						&:nth-child(2n-1) {
							border-right: 1rpx solid #eeeeee;
						}

						&:nth-child(2n) {
							padding-left: 30rpx;
						}

						&:nth-last-child(1) {
							padding-bottom: 15rpx;
						}

						&:nth-last-child(2) {
							padding-bottom: 15rpx;
							color: #333;
						}
					}
				}
			}
		}

		.search_goods_list {
			padding: 24rpx;
		}
	}
</style>